package com.example.myproject.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.common.ScanActivity;
import com.example.common.ToastUtil;
import com.example.common.ZXutils;
import com.example.myproject.R;
import com.google.zxing.Result;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2018/4/4 0004.
 * <p>
 * 资讯界面
 * <p>
 * 跳转扫一扫
 */

public class InformationFragment extends Fragment implements View.OnClickListener {

    private Button but_create_qr;
    private Button but_see_qr;
    private Button but_image_qr;
    private Button but_clear_imag;
    private TextView text_show_result;
    private ImageView iv_imag_qr;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_information, null);
        but_create_qr = rootView.findViewById(R.id.but_create_qr);
        but_create_qr.setOnClickListener(this);
        but_see_qr = rootView.findViewById(R.id.but_see_qr);
        but_see_qr.setOnClickListener(this);
        but_image_qr = rootView.findViewById(R.id.but_image_qr);
        but_image_qr.setOnClickListener(this);
        but_clear_imag = rootView.findViewById(R.id.but_clear_imag);
        but_clear_imag.setOnClickListener(this);

        text_show_result = rootView.findViewById(R.id.text_show_result);
        iv_imag_qr = rootView.findViewById(R.id.iv_imag_qr);

        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but_create_qr://生成二维码并显示
                String aStr = "发现新大陆";
                Bitmap bitmap = ZXutils.createBitmap(aStr);
                text_show_result.setText("生成文字：" + aStr);
                iv_imag_qr.setImageBitmap(bitmap);
                break;
            case R.id.but_see_qr:
                //识别文字
                new IntentIntegrator(getActivity())
                        .setOrientationLocked(false)
                        .setCaptureActivity(ScanActivity.class) // 设置自定义的activity是ScanActivity
                        .initiateScan(); // 初始化扫描
                break;
            case R.id.but_image_qr:
                InputStream inputStream = getResources().openRawResource(R.raw.timg_qr);
                byte[] buffer = null;
                try {
                    buffer = new byte[inputStream.available()];
                    inputStream.read(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (inputStream != null)
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                }
                Bitmap byteArray = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
                Result result = ZXutils.getReultFromBitmap(byteArray);
                //图中文字
                if (result != null) {
                    text_show_result.setText("图中文字：" + result.getText());
                    iv_imag_qr.setImageBitmap(byteArray);
                } else {
                    ToastUtil.show(getActivity(), "识别失败");
                }
                break;
            case R.id.but_clear_imag:
                text_show_result.setText("清空：");
                iv_imag_qr.setImageResource(R.mipmap.ic_launcher);
                break;
            default:
                Log.i("tags", "未注册id");
                break;
        }
    }

    // 通过 onActivityResult的方法获取扫描回来的值
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            if (intentResult.getContents() == null) {
                ToastUtil.show(getContext(), "内容为空");
            } else {
                ToastUtil.show(getContext(), "扫描成功");
                String ScanResult = intentResult.getContents();
                byte[] rawBytes = intentResult.getRawBytes();
                //bitmap生成，万一炸了呢？
                Bitmap bitmap = BitmapFactory.decodeByteArray(rawBytes, 0, rawBytes.length);
                text_show_result.setText("识别文字：" + ScanResult);
                iv_imag_qr.setImageBitmap(bitmap);
                Log.e("tags", "识别结果：" + intentResult.toString());
//                识别结果：Format: QR_CODE
//                Contents: hahahaha
//                Raw bytes: (19 bytes)   这里是什么呢？
//                Orientation: null
//                EC level: L
//                Barcode image: null
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

}
